<?php
class Comment extends Model {
	public function getCommentList($timeStamp, $pageType, $pageSize, $answerId) {
		if ($timeStamp == "null" || $timeStamp == "" || $timeStamp == "NULL") {
			$sql = "SELECT 	`comment_id` AS `commentID`,
				`comment_create_userid` AS `commentFromId`,
				`usera`.`user_nickname` AS `commentFromName`,
				IFNULL(`comment_to_userid`, '') AS `commentToId`,
				IFNULL(`userb`.`user_nickname`, '') AS `commentToName`,
				`comment_text` AS `commentContent`,
				`comment_createtime` AS `commentCreateTime`,
				`comment_id` AS `timeStamp`,
				IFNULL(`comment_score`, '') AS `commentFraction`
			FROM 	`ccomment`
			LEFT JOIN `uuser` usera
			ON 		`comment_create_userid`=`usera`.`user_id`
			LEFT JOIN `uuser` userb
			ON		`comment_to_userid`=`userb`.`user_id`
			WHERE	`answer_id` = $answerId
			ORDER BY `comment_id` DESC LIMIT $pageSize";
		} else if ($pageType == "pre") {
			$sql = "SELECT 	`comment_id` AS `commentID`,
				`comment_create_userid` AS `commentFromId`,
				`usera`.`user_nickname` AS `commentFromName`,
				IFNULL(`comment_to_userid`, '') AS `commentToId`,
				IFNULL(`userb`.`user_nickname`, '') AS `commentToName`,
				`comment_text` AS `commentContent`,
				`comment_createtime` AS `commentCreateTime`,
				`comment_id` AS `timeStamp`,
				`comment_score` AS `commentFraction`
			FROM 	`ccomment`
			LEFT JOIN `uuser` usera
			ON 		`comment_create_userid`=`usera`.`user_id`
			LEFT JOIN `uuser` userb
			ON		`comment_to_userid`=`userb`.`user_id`
			WHERE	`answer_id` = $answerId
			AND		`comment_id` > $timeStamp
			ORDER BY `comment_id` DESC LIMIT $pageSize";
		} else {
			$sql = "SELECT 	`comment_id` AS `commentID`,
				`comment_create_userid` AS `commentFromId`,
				`usera`.`user_nickname` AS `commentFromName`,
				IFNULL(`comment_to_userid`, '') AS `commentToId`,
				IFNULL(`userb`.`user_nickname`, '') AS `commentToName`,
				`comment_text` AS `commentContent`,
				`comment_createtime` AS `commentCreateTime`,
				`comment_id` AS `timeStamp`,
				`comment_score` AS `commentFraction`
			FROM 	`ccomment`
			LEFT JOIN `uuser` usera
			ON 		`comment_create_userid`=`usera`.`user_id`
			LEFT JOIN `uuser` userb
			ON		`comment_to_userid`=`userb`.`user_id`
			WHERE	`answer_id` = $answerId
			AND		`comment_id` < $timeStamp
			ORDER BY `comment_id` DESC LIMIT $pageSize";
		}
		// echo $sql;
		return $this->fetchAll ( $sql );
	}
	public function getCommentState($commentFromID, $answerID) {
		$sql = "SELECT evalue FROM evalue WHERE user_id=$commentFromID AND answer_id=$answerID";
		// echo $sql;
		return $this->fetchRow ( $sql );
	}
	public function addStudentComment($commentText, $commentFromID, $answerID, $score) {
		$sql = "INSERT INTO `ccomment` (`comment_text`,`comment_create_userid`,`answer_id`,`comment_score`,`comment_createtime`)
		VALUES ('$commentText', $commentFromID, $answerID, $score, NOW())";
		return $this->insert ( $sql );
	}
	public function addTeacherComment($commentText, $commentFromID, $commentToID, $answerID) {
		$sql = "INSERT INTO `ccomment`
			(`comment_text`,`comment_create_userid`,`comment_to_userid`,`answer_id`, `comment_createtime`)
			VALUES
			('$commentText', $commentFromID, $commentToID, $answerID, NOW())";
		// echo $sql;
		return $this->insert ( $sql );
	}
	
	/**
	 * 子评论
	 * 
	 * @param unknown_type $cid        	
	 * @return Ambigous <boolean, multitype:>
	 */
	public function getChildComment($userParentId, $commentParentId) {
		$sql = "SELECT `ccomment`.*, `uuser`.`user_realname` FROM `ccomment` " . "LEFT JOIN `uuser` ON `ccomment`.`comment_create_userid` = `uuser`.`user_id` " . "WHERE `ccomment`.`comment_to_userid` = '" . $userParentId . "' AND `ccomment`.`comment_parent_id` = '" . $commentParentId . "'";
		// echo $sql;
		return $this->fetchAll ( $sql );
	}
	
	/**
	 * 根据ID删除评论
	 * 
	 * @param unknown_type $commentId        	
	 * @return resource
	 */
	public function delCommentByCommentId($commentId) {
		$sql = "DELETE FROM `howdo`.`ccomment` WHERE `ccomment`.`comment_id` = '" . $commentId . "'";
		return $this->del ( $sql );
	}
	
	/**
	 * 父节点下所有的子评论
	 * 
	 * @param unknown_type $parentId        	
	 */
	public function allChildByParentId($parentId) {
		$sql = "SELECT b.* FROM ccomment a,ccomment b WHERE a.comment_id <= b.comment_parent_id AND a.comment_id = '" . $parentId . "'";
		return $this->fetchAll ( $sql );
	}
	
	/**
	 * 根据视频答案ID获取该视频的用户评论
	 * 
	 * @param unknown_type $answerId        	
	 * @param unknown_type $page        	
	 * @param unknown_type $num        	
	 * @return multitype:unknown number Ambigous <boolean, multitype:>
	 */
	public function getCommentPageModel($answerId, $page = 1, $num = 10) {
		$sql = "SELECT `answer`.`answer_create_time`,`answer`.`answer_create_userid`, `ccomment`.*,a.`user_nickname`, b.`user_nickname` AS `comment_to_user_nickname` FROM `ccomment` " 
			 . "LEFT JOIN `answer` ON `answer`.`answer_id` = `ccomment`.`answer_id` " 
			 . "LEFT JOIN `uuser` a ON `ccomment`.`comment_create_userid` = a.`user_id` " 
			 . "LEFT JOIN `uuser` b ON `ccomment`.`comment_to_userid` = b.`user_id` " 
			 . "WHERE `ccomment`.`answer_id` = '" . $answerId . "' " 
			 . "LIMIT " . ($page - 1) * $num . ", " . $num . " ";
		$sqlTotal = "SELECT `answer`.`answer_create_time`,`answer`.`answer_create_userid`, `ccomment`.*,a.`user_nickname`, b.`user_nickname` AS `comment_to_user_nickname` FROM `ccomment` " 
			 	  . "LEFT JOIN `answer` ON `answer`.`answer_id` = `ccomment`.`answer_id` " 
			      . "LEFT JOIN `uuser` a ON `ccomment`.`comment_create_userid` = a.`user_id` " 
			      . "LEFT JOIN `uuser` b ON `ccomment`.`comment_to_userid` = b.`user_id` " 
			      . "WHERE `ccomment`.`answer_id` = '" . $answerId . "' " ;
		// echo $sqlTotal;
		$list = $this->fetchAll ( $sql );
		$total = count ( $this->fetchAll ( $sqlTotal ) );
		$totalPage = ceil ( $total / $num );
		return array (
				'page' => $page,
				'list' => $list,
				'total' => $total,
				'totalPage' => $totalPage 
		);
	}
	public function getCommentStateByUserId($userID, $answerID) {
		$sql = "SELECT `ccomment`.`comment_id` FROM `ccomment` 
		Where `comment_create_userid`='$userID' AND `answer_id`='$answerID'";
		return $this->fetchRow ( $sql );
	}
}

?>